// 获取应用实例
const app = getApp()
var baseurl = app.baseurl.url;
Page({
  /**
   * 页面的初始数据
   */
  data: {
    yuming: app.yuming.url,
    //轮播图的数组
    backgroundArr: [],
    //轮播图当前的下标
    current: 0,
    //是否自动播放轮播图
    autoplay: false,
    flag: true,
    list: [],
    goods_id: '',
    collect: '',
    img: '',
    number: 1,
    // freight: '',// 运费
    // flag: 1
  },

  //监听轮播图的下标
  monitorCurrent: function (e) {
    // console.log(e.detail.current)
    let current = e.detail.current;
    this.setData({
      current: current
    })
  },
  buy() {
    let openid = wx.getStorageSync("openid")
    // 是否有openid登录
    if (openid != '') {
      wx.navigateTo({
        url: `/pages/confirm_order/confirm_order?goods_id=${this.data.goods_id}`,
      })
    } else {
      wx.showToast({
        title: '请先登录后，购买',
        icon: 'none',
        duration: 2000
      })

      setTimeout(() => {
        wx.reLaunch({
          url: '/pages/login/login'
        })
      }, 500);

    }

  },

  home() {
    wx.switchTab({
      url: '/pages/index/index',
    })
  },
  car() {
    wx.switchTab({
      url: '/pages/car/car',
    })
  },
  mark() {
    this.setData({
      flag: true
    })
  },
  join_car() {
    this.setData({
      flag: false
    })
  },
  add() {
    let num = this.data.number
    if (num < 999) {
      num++

      this.setData({
        number: num
      });

    } else {
      wx.showToast({
        title: '库存不足',
        icon: 'none',
        duration: 2000
      })
    }
  },
  reduce() {
    let num = this.data.number
    if (num > 1) {
      num--

      this.setData({
        number: num
      });

    } else {
      wx.showToast({
        title: '至少数量为1件',
        icon: 'none',
        duration: 2000
      })
    }
  },
  // 加入购物车
  sure() {
    this.setData({
      flag: true
    })
    wx.request({
      url: `${baseurl}/good.ShoppingCart/cart_add`,
      method: 'POST',
      data: {
        goods_id: this.data.goods_id,
        number: this.data.number,
        user_id: wx.getStorageSync("user_id"),
        openid: wx.getStorageSync("openid")
      },
      success: (res) => {
        if (res.data.code==600) {
          wx.reLaunch({
            url: '/pages/login/login'
          })
        }
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 2000
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    // console.log(options.id);
    // console.log('222',options);
    // 详情列表
    this.setData({
      goods_id: options.id
    })
    this.list(this.data.goods_id)
  },

  // 详情
  list(id) {
    wx.request({
      url: `${baseurl}/good.Goods/good_details`,
      method: 'POST',
      data: {
        goods_id: id,
        user_id: wx.getStorageSync("user_id"),
      },
      success: (res) => {
        // console.log('list:', res.data.data.collect);
        // if (res.data.data.collect == null) {
        //   this.setData({
        //     flag: 1
        //   })
        // } else {
        //   this.setData({
        //     flag: 2
        //   })
        // }
        const CONTENT = res.data.data.data.descript.replace(
          /\<img/gi,
          '<img style="width:100%;height:auto" '
        );
        this.setData({
          list: res.data.data,
          descript:CONTENT,
          backgroundArr: res.data.data.data.image,
          img: res.data.data.data.image[0],
          collect: res.data.data.collect
        })
      }
    })
    // 默认地址
    wx.request({
      url: `${baseurl}/user.UserAddress/get_details`,
      method: 'POST',
      data: {
        user_id: wx.getStorageSync("user_id"),
        status: 1
      },
      success: (res) => {
        // console.log(res.data.data);
        this.setData({
          address: res.data.data
        })
        // this.freight()
      }
    })
  },
  // 运费
  // freight() {
  //   wx.request({
  //     url: `${baseurl}/order.Order/Freight`,
  //     method: 'POST',
  //     data: {
  //       user_id: wx.getStorageSync("user_id"),
  //       province: this.data.address.province,
  //     },
  //     success: (res) => {
  //       // console.log(res.data);
  //       this.setData({
  //         freight: res.data.data.data
  //       })
  //     }
  //   })
  // },
  // 添加收藏
  collet() {
    var that = this
    if (this.data.collect == null) {
      console.log('111', this.data.collect);
      // 添加
      wx.request({
        url: `${baseurl}/user.UserCollect/collect_add`,
        method: 'POST',
        data: {
          collect_id: this.data.goods_id,
          type: 1,
          user_id: wx.getStorageSync("user_id"),
          openid: wx.getStorageSync("openid"),
        },
        success: (res) => {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
          that.list(that.data.goods_id)
        }
      })

    } else {
      console.log('222', this.data.collect);

      wx.request({
        url: `${baseurl}/user.UserCollect/collect_del`,
        method: 'POST',
        data: {
          user_id: wx.getStorageSync("user_id"),
          id: this.data.collect,
        },
        success: (res) => {
          wx.showToast({
            title: res.data.msg,
            icon: 'none',
            duration: 2000
          })
          that.list(that.data.goods_id)
        }
      })
    }

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //开启轮播图
    this.setData({
      autoplay: true
    })

    // this.onLoad()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    //关闭轮播图
    this.setData({
      autoplay: false
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    //关闭轮播图
    this.setData({
      autoplay: false
    })
  },
})
